package com.winit.component.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.util.Constants;
import com.winit.component.model.ComponentLib;
import com.winit.indexing.model.Indexing;
import com.winit.indexing.model.IndexingId;
import com.winit.indexing.service.IIdexingService;
import com.winit.select.service.SelectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangjian on 2017/11/23.
 */
@Component
public class ComponentService {

    private static Logger log = LoggerFactory.getLogger(ComponentService.class);

    @Autowired
    SelectService service;
    @Autowired
    private IIdexingService indexService;

    public ComponentLib getComponentLib(String name, String indexGroup, boolean withChild) {
        Map<String, List<Map<String, String>>> map = service.getSelectConfigs(indexGroup, name);
        List<Map<String, String>> keys = map.get(name);
        ComponentLib lib = new ComponentLib();
        if (keys.size() > 0) {
            for (Map<String, String> m : keys) {
                if ("component".equalsIgnoreCase(m.get("key"))) {
                    JSONObject jsonObject = JSONObject.parseObject(m.get("value"));
                    lib.setComponent(jsonObject);
                } else if ("attrs".equalsIgnoreCase(m.get("key"))) {
                    JSONArray array = JSONArray.parseArray(m.get("value"));
                    List<Map> attrs = new ArrayList<>();
                    for (int i = 0; i < array.size(); i++) {
                        attrs.add((Map) JSONObject.toJSON(array.get(i)));
                    }
                    lib.setAttrs(attrs);
                } else if ("available_sub_component_id".equalsIgnoreCase(m.get("key"))) {
                    JSONArray array = JSONArray.parseArray(m.get("value"));
                    List<String> sub = new ArrayList<>();
                    List<Map> list = new ArrayList<>();
                    for (int i = 0; i < array.size(); i++) {
                        sub.add((String) array.get(i));
                        if (withChild) {
                            list.add(getComponentLib((String) array.get(i), Constants.element_component_index_group, false).getComponent());
                        }
                    }
                    lib.setAvailable_sub_component_id(sub);
                    lib.setAvailable_sub_component(list);
                }
            }
        }
        return lib;
    }

    public List<Map> getComponentList(){
        List<Map> l = new ArrayList<>();
        List<String> list = indexService.getFilterTag(Constants.element_component_index_group);
        for (String key:list){
            Map map = new HashMap();
            map.put("key",key);
            map.put("value",key);
            l.add(map);
        }
        return l;
    }

}
